package com.tw.academy.basic.$4_naming.practiceOne;

public class MorseCodeDecoder {
    private static final String SENTENCE_SEPARATOR = "\\s{2,}";
    private static final String EMPTY_STRING = "";
    private static final String SPACE = " ";
    private static final String NULL = "null";

    public static String decode(String input) {
        String[] encodedSentences = input.split(SENTENCE_SEPARATOR);
        StringBuilder result = new StringBuilder();
        for (String sentence : encodedSentences) {
            String [] codes = sentence.split(SPACE);
            for (String code: codes) {
                result.append(MorseCode.get(code));
            }
            result.append(SPACE);
        }
        return result.toString().replaceAll(NULL, EMPTY_STRING).trim();
    }
}
